import Vue from 'vue'
import Router from 'vue-router'
import routes from './routers'
import store from '../store'
import axios from 'axios'
import 'url-search-params-polyfill'
import _ from 'lodash'

Vue.use(Router)
// history hash
const router = new Router({
    mode: 'hash',
    routes
})
let LOGIN_PAGE_NAME = 'login'
let ipv6 = ''

router.beforeEach((to, from, next) => {
    let token_1 = to.query.token;
    let token = localStorage.getItem('TOKEN_KEY')
    if(token_1!=null){
        localStorage.setItem('TOKEN_KEY', token_1)
        store.dispatch('getUserInfo')
            .then(res => {
                next()
            })
            .catch(res => {
                //获取不到用户信息就停留在首页，点击标签的时候再跳转到登录页
                // console.log(res)
                // console.log("00000000>>>>>"+to.name)
                // next({ name: 'index' })
            })
    }
    if(token==null && to.name!='login'){
        // 先获取到IPV6地址
        store.dispatch('getIpAddress')
            .then(res => {
                if (res.proto == "ipv6") {
                    ipv6 = res.address;
                    localStorage.setItem('IP_ADS', ipv6)
                }
            })
        // 根据IP获取Token
        store.dispatch('getTokenByIp')
            .then(res=>{
                console.log(res)
                // next({ name: 'index' })
            }).catch(res=>{
                if(to.name !== 'index'){
                    next(LOGIN_PAGE_NAME)
                }
            })
        token = localStorage.getItem('TOKEN_KEY')
        if(token==null){
            store.dispatch('getIpFreeToken')
                .then(res=>{
                    console.log(res)
                    next({ name: 'index' })
                }).catch(res => {
                    if(to.name !== 'index'){
                        next(LOGIN_PAGE_NAME)
                    }
                })
        }
    }
    token = localStorage.getItem('TOKEN_KEY')
    if (to.name === 'register') { // 跳转注册页面
        next() // 跳转
    } else if (!token && (to.name !== 'index' && to.name !== LOGIN_PAGE_NAME)) {
        next({ name: 'index' })
    } else if (!token && (to.name === 'index' || to.name == LOGIN_PAGE_NAME)) {
        if (!_.isEmpty(store.state.app.userInfo)) {
            next()
        } else if (_.isEmpty(store.state.app.userInfo) && to.name == LOGIN_PAGE_NAME){
            store.dispatch('geUntLoginMenus')
                .then(res => {
                    next()
                })
        } else {
            next()
        }
    } else {
        if (!_.isEmpty(store.state.app.userInfo)) {
            next()
        } else {
            store.dispatch('getUserInfo')
                .then(res => {
                    if(to.query.tablename==undefined && window.location.hostname.indexOf("www") == -1){
                        let param = res.data.user.params;
                        let breadcrumb = []
                        // url+","+tableName+","+icon+","+status+","+tagName+","+name;
                        // menuType=1291&status=0&breadcrumb="三新"经济数据库,三新经济数据&headTag="三新"经济数据库
                        if(param!=undefined){
                            let params = param.split(",")
                            let url = params[0]
                            let tableName = params[1]
                            let icon = params[2]
                            let status = params[3]
                            let tagName = params[4]
                            let name = params[5]
                            if(status==3){
                                breadcrumb.push("分省月度数据")
                                breadcrumb.push("分省月度数据")
                            } else {
                                breadcrumb.push(name)
                                breadcrumb.push(name)
                            }
                            next(url+'?tablename='+tableName+'&menuType='+icon+'&status='+status+'&headTag='+tagName+'&breadcrumb='+breadcrumb)
                        }else{
                            next()   
                        }
                    }else{
                        next()
                    }
                    // next()
                })
                .catch(res => {
                    //获取不到用户信息就停留在首页，点击标签的时候再跳转到登录页
                    // console.log(res)
                    // console.log("00000000>>>>>"+to.name)
                    next({ name: 'index' })
                })
        }
    }
});
//   if(to.name === 'register'){  // 跳转注册页面
//     next() // 跳转
//   }else if (!token && to.name !== LOGIN_PAGE_NAME ) {
//     // 未登录且要跳转的页面不是登录页
//     // next({name: LOGIN_PAGE_NAME})
//         // store.dispatch('geUntLoginMenus')
//         // next({name: 'index'})
//   } else if (!token && to.name === LOGIN_PAGE_NAME) {
//     // 未登陆且要跳转的页面是登录页
//     next() // 跳转
//   } else if (token && to.name === LOGIN_PAGE_NAME) {
//     next({   // 已登录且要跳转的页面是登录页
//       name: 'index' // 跳转到index页
//     })
//   } else {
//     if (!_.isEmpty(store.state.app.userInfo)) {
//       next()
//     } else {
//       store.dispatch('getUserInfo')
//         .then(res => {
//           next()
//         })
//         .catch(res => {
//           next(LOGIN_PAGE_NAME)
//         })
//     }
//   }
// });
export default router